﻿// -------------------------------------------------------------------------- //
//	文件名		:	E:\etimprove\etimprove\suite_recalcall\suite_recalcall.cs
//	创建者		:	xianglinjian
//	创建时间	: 	2011-9-14	16:09
//	功能描述	:	
//
// -------------------------------------------------------------------------- //
#if ENABLE_ET_COM
#define ENABLE_AutoItX
namespace suite_recalcall
{

using System.Collections.Generic;
class XRecalcAll : suit_base.SuiteBase
{
    private string m_sSrcPath;  // 根目录
    private string m_sDstPath;
    private string m_sEtPath;
    private autoIt.CAutoIt m_wndKill;
    private Dictionary<string, List<tool.CAddInfoHlp.CFileInfo>> m_fileDic;

    public override string GetCommand()
    {
        return ET_Improve.Global.CMD_RECALCALL;
    }

    public override void Call()
    {
        try
        {
            LoadConfig();
            etOp.EtOp.ReRegisterET(m_sEtPath);
            m_fileDic = tool.CAddInfoHlp.GetFolderFiles(m_sSrcPath, ET_Improve.Global.CMD_RECALCALL);

            m_wndKill = new autoIt.CAutoIt();
            m_wndKill.Start();
            tool.LogTool.GetInstance().BeginUseLog(null, ET_Improve.Global.s_curCmdName);
            foreach (string sDir in m_fileDic.Keys)
            {
                foreach (tool.CAddInfoHlp.CFileInfo fi in m_fileDic[sDir])
                {
                    tool.Output.DumpWriteLine("> RecalcAll:" + fi.FileName);
                    RecalcAll(fi);
                }
            }
            tool.LogTool.GetInstance().EndUseLog();
            tool.Output.DumpWriteLine(">>>>>>recalc completed!<<<<<<");
        }
        catch (tool.ETImpException ex)
        {
            ex.AddLog();
        }
        catch (System.Exception ex)
        {
            tool.Output.DumpWriteLine(ex.ToString());
        }
        finally
        {
            m_wndKill.Stop();
        }
    }

    protected void RecalcAll(tool.CAddInfoHlp.CFileInfo fi)
    {
        etOp.EtOp et = new etOp.EtOp();
        try
        {
            m_wndKill.BeginMonitor(true);
            et.Init(m_wndKill, true);
            et.Open(fi.FileName, fi.Password);
            et.RecalcAll();
            string sRelPath = fi.FileName.Substring(m_sSrcPath.Length);
            sRelPath = m_sDstPath + sRelPath;
            System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(sRelPath));
            et.SaveAsFile(sRelPath);
            et.Close();
        }
        catch (tool.ETImpException ex)
        {
            if (ex.FileName == null)
            {
                ex.FileName = fi.FileName;
            }
            ex.AddLog();
        }
        catch (System.Exception ex)
        {
            tool.LogTool.GetInstance().AddLog(tool.LogTool.LOG_TYPE.EXCEPTION, fi.FileName, null, ex);
        }
        finally
        {
            m_wndKill.EndMonitor();
            etOp.EtOp.DestoryET(ref et);
        }
    }

    protected void LoadConfig()
    {
        const string LOG_ROOT = "RecalcAll";
        const string LOG_SRC_PATH = "SrcPath";
        const string LOG_DST_PATH = "DstPath";
        const string LOG_ET = "EtPath";
        try
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            System.Xml.XmlNodeList xnl = tool.ConfigReader.GetElementList(LOG_ROOT);
            foreach (System.Xml.XmlNode ele in xnl)
            {
                if (ele.Name == LOG_SRC_PATH)
                {
                    m_sSrcPath = ele.InnerText;
                }
                else if (ele.Name == LOG_DST_PATH)
                {
                    m_sDstPath = ele.InnerText;
                }
                else if (ele.Name == LOG_ET)
                {
                    m_sEtPath = ele.InnerText;
                }
            }
        }
        catch (System.Exception ex)
        {
            tool.Output.DumpWriteLine(ex.ToString());
            throw ex;
        }
    }

    protected class XLog
    {
        struct Record
        {
            string sFileName;
            int nOpenTM;
            int nFristRcTm;
            int nSecondRcTm;
            public Record(string s, int nop, int fir, int sec)
            {
                sFileName = s;
                nOpenTM = nop;
                nFristRcTm = fir;
                nSecondRcTm = sec;
            }
        }

//         protected List<Record> m_data;
// 
//         public XLog()
//         {
//             m_data = new List<Record>();
//         }
// 
//         // 有一项不正常就不通过
//         public void AddRecord(string sFileName, int nOpenTM, int nFirstRCTM, int nSecondRCTM)
//         {
//             Record r = new Record(sFileName, nOpenTM, nFirstRCTM, nSecondRCTM);
//             m_data.Add(r);
//         }
// 
//         public void SaveAs()
//         {
// 
//         }
    }
}

}
#endif // #if ENABLE_ET_COM